package com.ktgj.customer.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.ktgj.common.core.annotation.Excel;
import com.ktgj.common.core.utils.DateUtils;
import com.ktgj.customer.domain.Department;
import com.ktgj.customer.domain.vo.MallCategoryVo;
import com.ktgj.system.api.domain.SysDept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktgj.customer.mapper.MallCategoryMapper;
import com.ktgj.customer.domain.MallCategory;
import com.ktgj.customer.service.IMallCategoryService;

/**
 * 类目Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-05-30
 */
@Service
public class MallCategoryServiceImpl implements IMallCategoryService 
{
    @Autowired
    private MallCategoryMapper mallCategoryMapper;

    /**
     * 查询类目
     * 
     * @param id 类目ID
     * @return 类目
     */
    @Override
    public MallCategory selectMallCategoryById(Long id)
    {
        return mallCategoryMapper.selectMallCategoryById(id);
    }

    /**
     * 查询类目列表
     * 
     * @param mallCategory 类目
     * @return 类目
     */
    @Override
    public List<MallCategory> selectMallCategoryList(MallCategory mallCategory)
    {
        return mallCategoryMapper.selectMallCategoryList(mallCategory);
    }

    /**
     * 新增类目
     * 
     * @param mallCategory 类目
     * @return 结果
     */
    @Override
    public int insertMallCategory(MallCategory mallCategory)
    {
        return mallCategoryMapper.insertMallCategory(mallCategory);
    }

    /**
     * 修改类目
     * 
     * @param mallCategory 类目
     * @return 结果
     */
    @Override
    public int updateMallCategory(MallCategory mallCategory)
    {
        mallCategory.setUpdateTime(DateUtils.getNowDate());
        return mallCategoryMapper.updateMallCategory(mallCategory);
    }

    /**
     * 批量删除类目
     * 
     * @param ids 需要删除的类目ID
     * @return 结果
     */
    @Override
    public int deleteMallCategoryByIds(Long[] ids)
    {
        return mallCategoryMapper.deleteMallCategoryByIds(ids);
    }

    /**
     * 删除类目信息
     * 
     * @param id 类目ID
     * @return 结果
     */
    @Override
    public int deleteMallCategoryById(Long id)
    {
        return mallCategoryMapper.deleteMallCategoryById(id);
    }

    /**
     * 树型商品类目
     * */
    @Override
    public List<MallCategoryVo> mallCategoryList() {
        //获取根节点（获取所有的父节点）
        List<MallCategoryVo> lstData = new ArrayList<>();
        List<MallCategory> mallCategories = mallCategoryMapper.mallCategoryList();
        for (MallCategory mallCategory : mallCategories) {
            if (mallCategory.getPid() == 0) {
                MallCategoryVo mallCategoryVo = new MallCategoryVo();
                mallCategoryVo.setId(mallCategory.getId());
                mallCategoryVo.setName(mallCategory.getName());
                mallCategoryVo.setPid(mallCategory.getPid());
                getChildDepartment(mallCategoryVo, mallCategories);
                lstData.add(mallCategoryVo);
            }
        }
        return lstData;
    }

    public void getChildDepartment(MallCategoryVo mallCategory, List<MallCategory> lstDepartment) {
        for (MallCategory tmpMallCategory : lstDepartment) {
            if (tmpMallCategory.getPid().equals(mallCategory.getId())) {
                MallCategoryVo mallCategoryVo = new MallCategoryVo();
                mallCategoryVo.setId(tmpMallCategory.getId());
                mallCategoryVo.setName(tmpMallCategory.getName());
                mallCategoryVo.setPid(tmpMallCategory.getPid());
                mallCategory.getMallCategoryVoList().add(mallCategoryVo);
            }
        }
        for (MallCategoryVo child : mallCategory.getMallCategoryVoList()) {
            getChildDepartment(child, lstDepartment);
        }
    }
}
